extends Node

var last_level_path
var last_level_score = 0

var _save: SaveData

func _ready() -> void:
	_create_or_load_save()
	#_save_score()
	
func load_level(level_path):
	if not ResourceLoader.exists(level_path):
		return
	last_level_path = level_path
	_create_or_load_save()
	get_tree().change_scene_to_file(level_path)
	

func _create_or_load_save() -> void:
	
	if SaveData.save_exists():
		_save = SaveData.load_score_data()
		print("current level: " + str(last_level_path) + "-  current score: " + str(_save.get_highscore_entry(last_level_path)))
		

	else:
		_save = SaveData.new()
		#_save.level01_score = last_level_score
		print ("saving score...") 
	
		_save.save_score_data()

func save_score():
	#print (SaveData.load_score_data())
	#if last_level_score > _save.last_score:
	_save.last_score = last_level_score
	_save.level_name = last_level_path
	_save.save_score_data()

func get_highscore_entry(_level_name):
	return _save.get_highscore_entry(_level_name)
